from sqlalchemy import Column, String, Text, DateTime, Boolean, Integer, Float, JSON
from sqlalchemy.sql import func
from app.models.video import Base
from typing import Dict, Any, Optional
import uuid


class Transcription(Base):
    """转录结果模型"""
    __tablename__ = "transcriptions"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    video_id = Column(String(36), nullable=False, index=True, comment="关联的视频ID")
    
    # 转录状态
    status = Column(String(20), default="pending", comment="转录状态: pending, processing, completed, failed")
    
    # ASR结果
    transcript_text = Column(Text, comment="完整转录文本")
    language_detected = Column(String(10), comment="检测到的语言代码")
    confidence_score = Column(Float, comment="整体置信度分数")
    
    # 说话人识别结果
    speaker_count = Column(Integer, default=0, comment="检测到的说话人数量")
    speaker_mapping = Column(JSON, comment="说话人映射信息")
    
    # 时间戳信息
    duration = Column(Float, comment="音频总时长(秒)")
    word_count = Column(Integer, default=0, comment="单词数量")
    
    # 处理信息
    processing_time = Column(Float, comment="处理耗时(秒)")
    error_message = Column(Text, comment="错误信息")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        return {
            'id': self.id,
            'video_id': self.video_id,
            'status': self.status,
            'transcript_text': self.transcript_text,
            'language_detected': self.language_detected,
            'confidence_score': self.confidence_score,
            'speaker_count': self.speaker_count,
            'speaker_mapping': self.speaker_mapping,
            'duration': self.duration,
            'word_count': self.word_count,
            'processing_time': self.processing_time,
            'error_message': self.error_message,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }


class TranscriptionSegment(Base):
    """转录片段模型 - 存储带时间戳的转录片段"""
    __tablename__ = "transcription_segments"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    transcription_id = Column(String(36), nullable=False, index=True, comment="关联的转录ID")
    
    # 片段信息
    start_time = Column(Float, nullable=False, comment="开始时间(秒)")
    end_time = Column(Float, nullable=False, comment="结束时间(秒)")
    text = Column(Text, nullable=False, comment="片段文本")
    confidence = Column(Float, comment="置信度分数")
    
    # 说话人信息
    speaker_id = Column(String(20), comment="说话人ID")
    speaker_label = Column(String(50), comment="说话人标签")
    
    # 语言信息
    language = Column(String(10), comment="语言代码")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        return {
            'id': self.id,
            'transcription_id': self.transcription_id,
            'start_time': self.start_time,
            'end_time': self.end_time,
            'text': self.text,
            'confidence': self.confidence,
            'speaker_id': self.speaker_id,
            'speaker_label': self.speaker_label,
            'language': self.language,
            'created_at': self.created_at.isoformat() if self.created_at else None
        } 